package com.woniuxy.main.mapper;

import com.woniuxy.main.pojo.Order;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Copyright (C), 2019-9012, sun sss info. Co., Ltd.
 *
 * @author Su
 * @version 1.0
 * InterfaceName: OrderMapper
 * Description: 订单持久层接口
 * @date 2019/8/1 12:18
 **/
@Repository
public interface OrderMapper {


    /**
     * 查询:所有订单,并获取订单中所属用户和员工全部信息
     *
     * @return List<Order>
     * @throws Exception 全部
     */
    @Select("select * from `order`")
    @Results(id = "orderMap", value = {
            @Result(id = true, column = "order_id", property = "order_id"),
            @Result(column = "ordernum", property = "ordernum"),
            @Result(column = "paynum", property = "paynum"),
            @Result(column = "money", property = "money"),
            @Result(column = "deposit", property = "deposit"),
            @Result(column = "otherexp", property = "otherexp"),
            @Result(column = "trueexp", property = "trueexp"),
            @Result(column = "orderdate", property = "orderdate"),
            @Result(column = "startdate", property = "startdate"),
            @Result(column = "enddate", property = "enddate"),
            @Result(column = "day", property = "day"),
            @Result(column = "comment", property = "comment"),
            @Result(column = "star", property = "star"),
            @Result(column = "remark", property = "remark"),
            @Result(column = "roomtype", property = "roomtype"),
            @Result(column = "roomnum", property = "roomnum"),
            @Result(column = "status", property = "status"),
            @Result(column = "flag", property = "flag"),
            @Result(column = "user_id", property = "user_id"),
            @Result(column = "staff_id", property = "staff_id"),
            @Result(property = "user", column = "user_id", one = @One(
                    select = "com.woniuxy.main.mapper.UserMapper.findById", fetchType = FetchType.EAGER)),
            @Result(property = "staff", column = "staff_id", one = @One(
                    select = "com.woniuxy.main.mapper.StaffMapper.selectStaffById", fetchType = FetchType.EAGER))
    })
    List<Order> findAll() throws Exception;


    /**
     * 查询:根据订单主键id 查询订单
     *
     * @param order_id 订单主键id
     * @return order对象
     * @throws Exception 全部
     */
    @Select("select * from `order`  where order_id=#{order_id}")
    Order findById(String order_id) throws Exception;

    /*
    结账时的订单

     */
    @Select("select * from `order`  where order_id=#{order_id} ")
    Order findByIdAndStauts(String order_id) throws Exception;
    /**
     * 查询:根据订单号 模糊查询订单
     *
     * @param ordernum
     * @return
     * @throws Exception
     */
    @Select("select * from `order`  where ordernum like #{ordernum}")
    List<Order> findByOrdernum(String ordernum) throws Exception;

    /**
     * 查询:根据订单预定时间 查询当天所有订单id
     *
     * @param orderdate
     * @return
     * @throws Exception
     */
    @Select("select order_id from `order`  where orderdate like #{orderdate}")
    List<Integer> findByOrderdate(String orderdate) throws Exception;


    /**
     * 新增:订单
     * ordernum   char(16) NULL      订单号
     * paynum     char(32) NULL      支付号
     * money      decimal(8,5) NULL  支付金额
     * deposit    int(11) NULL       押金
     * orderdate  char(32) NULL     预定时间
     * day        int(11) NULL      预定天数
     * user_id    int(11) NULL      用户_id
     * staff_id   int(11) NULL      员工_id
     * status     int(11) NULL      订单状态
     * 0等待付款 1已支付押金 2预约成功（待确认入住）
     * 3预约失败 4入住成功   5申请取消订单
     * 6已退款   7取消成功   8订单完成 9订单完成（已评论）
     *
     * @param order 订单对象
     * @throws Exception 全部
     */
    @Insert("insert into `order`" +
            "(ordernum,paynum,payway,money,deposit,day,staff_id,status)" +
            " values" +
            "(#{ordernum},#{paynum},#{payway},#{money},#{deposit},#{day},#{staff_id},#{status})")
    boolean addOrder(Order order) throws Exception;

    /**
     * 修改:根据订单id修改订单状态
     *
     * @param order_id 订单id
     * @param status   要修改成的状态
     * @return
     * @throws Exception 全部
     */
    @Update("update `order` set status=#{status} where order_id=#{order_id}")
    boolean updateOrder(String order_id, Integer status) throws Exception;



}
